package com.sy.core.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.sy.core.web.BaseCtl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 阿里云OSS服务
 * @author asus
 *
 */
@Controller
@RequestMapping("aliOSS")
public class AliOSSCtl extends BaseCtl {
	
	private static final String AccessKeyId="LTAIMTGWY0bUzh0D";
	private static final String AccessKeySecret="ymgsGzmuTc9NyP2dKone3buKGx4q1W";
	
	private static final String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
	private static final String bucketName = "shinyoudianshang";
	/**
	 * 
	 * @param file
	 * @param type 业务模块
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "fileUpload")
	@ResponseBody
	public RestResult imageUpload(
			@RequestParam("file") MultipartFile[] file,
			@RequestParam(value = "type") String type,
			HttpServletRequest request, HttpServletResponse response) {
		
		// 创建OSSClient实例。
		OSSClient ossClient = new OSSClient(endpoint, AccessKeyId, AccessKeySecret);
		// 上传内容到指定的存储空间（bucketName）并保存为指定的文件名称（objectName）。
		FileNamePathEntry2 fnp=null;
		List<FileNamePathEntry2> fnps=new ArrayList<FileNamePathEntry2>();
		for(int i=0;i<file.length;i++){
			long fileSize = file[i].getSize();
			String uuid=UUID.randomUUID().toString();
			String originalFilename=file[i].getOriginalFilename();
			String hz=originalFilename.substring(originalFilename.lastIndexOf("."));
			fnp=new FileNamePathEntry2(originalFilename, bucketName+"."+"oss-cn-shanghai.aliyuncs.com/"+type+"/"
					+uuid+hz,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),fileSize);
			fnps.add(fnp);
			try {
				ossClient.putObject(bucketName, type+"/"+uuid+hz, file[i].getInputStream());
			} catch (OSSException | ClientException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		ossClient.shutdown();
		
		
		/*String content = "Hello OSS";
		ossClient.putObject(bucketName, "test/test2.txt", new ByteArrayInputStream(content.getBytes()));
		ossClient.shutdown();*/
		
		return new RestResult(true,"OK",fnps);
		
	}
	
	/**
	 * 阿里云上传文件后返回给前端的数据结构
	 * @author asus
	 *
	 */
	class FileNamePathEntry2{
		
		
		String name;
		
		String path;
		
		String addTime;
		
		long fileSize;

		public FileNamePathEntry2(String name, String path, String addTime, long fileSize) {
			super();
			this.name = name;
			this.path = path;
			this.addTime = addTime;
			this.fileSize = fileSize;
		}

		public FileNamePathEntry2() {
			super();
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPath() {
			return path;
		}

		public void setPath(String path) {
			this.path = path;
		}

		public String getAddTime() {
			return addTime;
		}

		public void setAddTime(String addTime) {
			this.addTime = addTime;
		}

		public long getFileSize() {
			return fileSize;
		}

		public void setFileSize(long fileSize) {
			this.fileSize = fileSize;
		}
		
	}
	
}
